package cn.hd01.repository;

import java.math.BigDecimal;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.hd01.repository.entity.Integration;

@Repository
public interface IntegrationRepository extends JpaRepository<Integration, Integer> {
	
	Page<Integration> findAll(Specification<Integration> spec, Pageable pageable);
	
	@Modifying
	@Transactional
	@Query(value = "update Integration set integration = (?1 + integration), update_time = now() where customerId = ?2")
	int updateIntegration(BigDecimal integration, Integer customerId);
	
	@Modifying
	@Transactional
	@Query(value = "update Integration set used_integration = (?1 + used_integration), update_time = now() where customerId = ?2")
	int updateUsedIntegration(BigDecimal integration, Integer customerId);
	
	@Modifying
	@Transactional
	@Query(value = "delete from Integration where customerId = ?1")
	int deleteByCustomerId(Integer customerId);
	
	Integration findByCustomerId(Integer customerId);
}
